//
//  CHWXJLDetailsViewController.m
//  CarFile
//
//  Created by 陈浩 on 2017/4/11.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHWXJLDetailsViewController.h"
#import "CHWXJLDetailCell.h"
#import "CHTableHeaderView.h"
#import "JLDetailModel.h"
#import "JLDetailListModel.h"
#import "ProjectModel.h"
#import "PartsModel.h"
#import "CHPartViewCell.h"
@interface CHWXJLDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *datas;
@property (nonatomic ,strong) NSArray *detailTitls;
@property (nonatomic ,strong) NSArray *titles;
@property (nonatomic ,strong) NSArray *tempArr;
@property (nonatomic ,strong) NSArray *tempArr1;
@property (nonatomic ,strong) UILabel *contentLabel;
@property (nonatomic ,strong) NSMutableArray *projectArrs;
@property (nonatomic ,strong) NSMutableArray *partArrs;
@end

@implementation CHWXJLDetailsViewController

-(NSMutableArray *)partArrs{
    if (!_partArrs) {
        _partArrs = [NSMutableArray array];
    }
    return _partArrs;
}

-(NSMutableArray *)datas{
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
    
}

-(NSMutableArray *)projectArrs{
    if (!_projectArrs) {
        _projectArrs = [NSMutableArray array];
    }
    return _projectArrs;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"维修记录详情";
    self.view.backgroundColor = [UIColor whiteColor];
    self.titles = @[@"维修企业名称",@"车辆号码",@"车辆识别代码(VIN码)",@"送修日期",@"结算日期",@"结算编码",@"送修里程",@"故障信息",@"维修项目",@"维修配件"];
    
    [self setupTableView];
    
    [super showLoading:YES];
    
    [self getData];
}


-(void)setupTableView{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, UIScreenH - 64) style:UITableViewStylePlain];
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    tableView.sectionHeaderHeight = 44;
    tableView.dataSource = self;
    tableView.delegate = self;
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, 120)];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
    titleLabel.text = @"质保信息";
    titleLabel.font = [UIFont systemFontOfSize:14];
    [footView addSubview:titleLabel];
    tableView.tableFooterView = footView;
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, UIScreenW -20 , 80)];
    contentLabel.font = [UIFont systemFontOfSize:14];
    contentLabel.numberOfLines = 0;
    [footView addSubview:contentLabel];
    self.contentLabel = contentLabel;
    
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

-(void)getData{
    
    NSArray *arr = [NSArray array];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (_model) {
       arr = [self.model.settledate componentsSeparatedByString:@" "];
       params[@"sign"] = [DESCript encrypt:self.model.recordid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
        params[@"recordid"] = self.model.recordid;
        params[@"settledate"] = arr[0];
    }else{
        arr = [self.listModel.settledate componentsSeparatedByString:@" "];
        params[@"sign"] = [DESCript encrypt:self.listModel.recordid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
        params[@"recordid"] = self.listModel.recordid;
        params[@"settledate"] = arr[0];
    }
    
    arr = [self.model.settledate componentsSeparatedByString:@" "];
    params[@"areaid"] = self.model.areaid;
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
   
    [[Globle getInstance].service requestWithServiceName:@"lcipapprecordandetail" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        [super showLoading:NO];
        NSLog(@"%@",result);
        if ([result[@"code"] isEqualToString:@"1"]) {
            
            NSArray *arr = result[@"data"][@"carinfo"];
            NSDictionary *dict = arr[0];
            NSArray *results = @[dict[@"company"],dict[@"carno"],dict[@"carvin"],dict[@"repairdate"],dict[@"settledate"],dict[@"settleno"],dict[@"repairmile"],dict[@"faultdescript"],@"32" ,@"22"];
            for (NSDictionary *dict in result[@"data"][@"carpartslist"]) {
                
                PartsModel *model1 = [PartsModel objectWithKeyValues:dict];
                
                [self.partArrs addObject:model1];
                
            }
            
            for (NSDictionary *dict in result[@"data"][@"repairhourslist"]) {
           
                ProjectModel *model2 = [ProjectModel objectWithKeyValues:dict];
                [self.projectArrs addObject:model2];
                
            }
            
            //            NSArr
            self.contentLabel.text = dict[@"warrantymsg"];
            CGFloat height = [self getHeightLineWithString:dict[@"warrantymsg"] withWidth:UIScreenW -20 withFont:[UIFont systemFontOfSize:14]];
            
            self.tableView.tableFooterView.size = CGSizeMake(UIScreenW, height +40);
            self.contentLabel.size = CGSizeMake(UIScreenW - 20, height);

            for (int i = 0; i < self.titles.count; i++) {
                JLDetailModel *model = [[JLDetailModel alloc] init];
                model.title = self.titles[i];
                model.detailTitle = results[i];
                [self.datas addObject:model];
            }
            
            [self.tableView reloadData];
        }
    }];
    
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datas.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 
 
    JLDetailModel *model = self.datas[section];
    if (section == 8) {
      
        return model.isOpened ? self.projectArrs.count : 0;
    }else if (section == 9){
       
        return model.isOpened ? self.partArrs.count : 0;
        
    }
    return 0;

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 8) {
        
        CHWXJLDetailCell *cell = [CHWXJLDetailCell cellForTableView:tableView];
        cell.model = self.projectArrs[indexPath.row];
        return cell;
    }else if(indexPath.section == 9){
        
        CHPartViewCell *cell = [CHPartViewCell cellForTableView:tableView];
        cell.model = self.partArrs[indexPath.row];
        return cell;
    }
    return nil;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    CHTableHeaderView *view = [[CHTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, 44)];
    view.model = self.datas[section];
    
    view.block = ^{
        
        [self.tableView reloadData];
    };
    return view;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 8) {
        return 80;
    }else{
        return 120;
    }
}

//去掉UItableview headerview黏性(sticky)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView)
    {
        CGFloat sectionHeaderHeight = 64; //sectionHeaderHeight
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}


#pragma mark LongriseRouterDelegater代理回调方法
-(void)setParams:(id)obj block:(LongriseRouterBlock)block
{
    
    if (obj && [obj isKindOfClass:[NSMutableDictionary class]]) {
        
        NSMutableDictionary *dict = obj;
        self.model = dict[@"model"];
    }
}

#pragma mark - 根据字符串计算label高度
- (CGFloat)getHeightLineWithString:(NSString *)string withWidth:(CGFloat)width withFont:(UIFont *)font {
    
    //1.1最大允许绘制的文本范围
    CGSize size = CGSizeMake(width, 2000);
    //1.2配置计算时的行截取方法,和contentLabel对应
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:10];
    //1.3配置计算时的字体的大小
    //1.4配置属性字典
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:style};
    //2.计算
    //如果想保留多个枚举值,则枚举值中间加按位或|即可,并不是所有的枚举类型都可以按位或,只有枚举值的赋值中有左移运算符时才可以
    CGFloat height = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size.height;
    
    return height;
}

@end
